package demo

import (
	"fmt"
	"gitee.com/851537/gmodel"
	"gitee.com/851537/gmodel/corm"
	"github.com/buraksezer/consistent"
	"gorm.io/gorm"
)

var AdminCashLogRing *consistent.Consistent = gmodel.NewConsistentHashRing(&gmodel.ConsistentHashRingConfig{
	consistent.Config{gmodel.Hasher{}, 100, 40, 1.25},
	100,
}, "admin_cash_log")

// gorm标签 https://gorm.io/zh_CN/docs/models.html
type AdminCashLog struct {
	gmodel.BaseDb
	Id int64 `json:"id" gorm:"primaryKey"`

	AdminId       int64 `orm:"default(0);index" json:"admin_id"`
	Cash          int   `orm:"default(0)" json:"cash"`           //流水金额,单位:分,正数为进,负数为出
	AvailableCash int   `orm:"default(0)" json:"available_cash"` //发生之后,可用金额

	//type 1.trade_reward 2.take cash 3.refund 4.takePoint 5.fillerCash(补货金)
	Type        int8   `orm:"default(0)" json:"type"`
	DataTable   string `orm:"size(50)" json:"data_table"  gorm:"size:50"`
	DataId      int64  `orm:"default(0)" json:"data_id"`
	Remark      string `orm:"size(100)" json:"remark"  gorm:"size:100"`
	PayConfigId int64  `orm:"default(0)" json:"pay_config_id"`

	CreateTime int64 `json:"create_time"`
}

func (this *AdminCashLog) Db() *gorm.DB {
	return corm.ClientIns("log").Db
	//return this.Client("log").Db
}

func (this *AdminCashLog) IsHash() bool {
	return true
}

func (this *AdminCashLog) TableName() string {
	owner := AdminCashLogRing.LocateKey([]byte(fmt.Sprintf("%d", this.AdminId)))
	return fmt.Sprintf("%s%s", DbPrefix, owner.String())
	//return DbPrefix + "admin_cash_log"
}

func (this *AdminCashLog) GetId() int64 {
	return this.Id
}

func NewAdminCashLog(id ...int64) *AdminCashLog {
	if len(id) > 0 {
		return &AdminCashLog{Id: id[0]}
	} else {
		return &AdminCashLog{}
	}
}
